// import { setLoginStatusAct, setProfileAct } from './actions';
import { setCurrentSongStatus, setCurrentTime } from './actions';
import {
  // ONE_LEVEL_ROUTE_CHANGE,
  SET_COOKIEACT,
  SET_LOGINSTATUSACT,
  SET_PROFILEACT,
  TOGGLE_SHOW_LOGINBOX,
  TWO_LEVEL_ROUTE_CHANGE,
  SET_CURRENTSONGSLIST_FUN,
  SET_CURRENTSONG_FUN,
  SET_CURRENT_DETAIL,
  ERR_MESSAGE,
  SEND_LRC,
  SET_CURRENTTIME,
  SET_CURRENTSONG_STATUS,
  SET_LRCARR,
  REVICE_SONGLIST,
  REVICE_MP3List,
  REVICE_CURRENTSONG_DETAIL,
  REVICE_CURRENT_MP3,
  TOGGLE_PLAYSTATUSEND,
  ADD_MP3LIST,
  ADD_SONGLIST,
  ADD_BROTHERNODECHANGETIME
} from './constants'

const initRoutes = {
  level: [],
  twoLevel: [
    { name: "推荐", id: 1, isOn: true, path: "/discover/recommend" },
    { name: "排行榜", id: 2, isOn: false, path: "/discover/rank" },
    { name: "歌单", id: 3, isOn: false, path: "/discover/musiclist" },
    { name: "主播电台", id: 4, isOn: false, path: "/discover/discoverdj" },
    { name: "歌手", id: 5, isOn: false, path: "/discover/discovermusician" },
    { name: "新歌上架", id: 6, isOn: false, path: "/discover/newsong" },
  ],
  currentTwolevelId: 1
}
export const routes = (state = initRoutes, action) => {
  switch (action.type) {
    case TWO_LEVEL_ROUTE_CHANGE:
      console.log(action, 'action');
      let id = action.id
      let twoList = state.twoLevel
      twoList.forEach(item => {
        if (item.id == id) {
          item.isOn = true
        } else {
          item.isOn = false
        }
      })

      let data = {
        ...state,
        twoLevel: [...twoList]
      }
      return data
    default:
      return state
  }
}

const initUser = {
  isLogin: false,
  profile: {},
  isShowLoginBox: false,
  cookie: ''
}
export const userinfo = (state = initUser, action) => {
  // console.log(action, 'action');
  switch (action.type) {
    case TOGGLE_SHOW_LOGINBOX:
      console.log('toggleShowLoginBox action');
      return {...state, isShowLoginBox: !state.isShowLoginBox };
    case SET_LOGINSTATUSACT:
      return {...state, isLogin: action.payload }
    case SET_COOKIEACT:
      return {...state, cookie: action.payload }
    case SET_PROFILEACT:
      return {...state, profile: action.payload }
    default:
      return state;
  }
}

const initDoinginfo = {
  currentSongMp3: {},
  currentSongDetail: {},
  currentSongsList: [],
  err_msg: null,
  sendlrcDetail: {},
  currenttime: 0,
  isPlaying: false, //true 是指playing（播放中）
  lrcArrTotal: {},
  mp3List: [],
  onEnd: false,
  brotherNodeChangeTime: 1
}
export const currentDoingInfo = (state = initDoinginfo, action) => {
  switch (action.type) {
    case REVICE_SONGLIST:
      return {...state, currentSongsList: [...action.payload] }
    case REVICE_CURRENT_MP3:
      return {...state, currentSongMp3: {...action.payload } }
    case REVICE_CURRENTSONG_DETAIL:
      return {...state, currentSongDetail: {...action.payload } }
    case ERR_MESSAGE:
      console.log(action.payload, 'error');
      return {...state, err_msg: action.payload }
    case SEND_LRC:
      return {...state, sendlrcDetail: {...action.payload } }
    case SET_CURRENTSONG_STATUS:
      console.log(action.payload, 'SET_CURRENTSONG_STATUS');
      return {...state, isPlaying: action.payload }
    case REVICE_MP3List:
      console.log(action, 'action');
      return {...state, mp3List: [...action.payload] }
    case SET_LRCARR:
      console.log(action.payload, 'SET_LRCARR');
      let key = action.payload.key
      let data = Object.assign({}, state.lrcArrTotal)
      console.log(data, 'data');
      data[key] = action.payload.value
      return {...state, lrcArrTotal: {...data } }
    case TOGGLE_PLAYSTATUSEND:
      return {...state, onEnd: action.payload }
    case ADD_SONGLIST:
      let cList = [...state.currentSongsList]
      let pList = [...action.payload]
      cList.forEach((item1, index) => {
        pList.forEach(item2 => {
          if (item1.id === item2.id) {
            cList.splice(index, 1)
            console.log('已添加歌单的歌曲item2 = ', item2.name);
          }
        })
      });
      let currentSongsList = [...cList, ...pList]
      return {...state, currentSongsList }
    case ADD_MP3LIST:
      return {...state, currentSongMp3: {...action.payload } }
    case ADD_BROTHERNODECHANGETIME:
      console.log(action, 'ation');
      return {...state, brotherNodeChangeTime: state.brotherNodeChangeTime + 1 }
    default:
      return state
  }
}

const playerChangeDataInit = {
  currentTime: 0
}
export const playerChangeData = (state = playerChangeDataInit, action) => {
  switch (action.type) {
    case SET_CURRENTTIME:
      return {...state, currentTime: action.payload };
    default:
      return state;
  }
}